var photo_index_js = {
    layer: null,
    laytpl: null,
    upload: null,
    layerIndex: null,
    item: null,
    btn_init: function () {
        $('#photo_index_exit').on('click', function () {
            $('#album_index_div').show();
            $('#photo_index_div').remove();
        });
        var uploadInst = photo_index_js.upload.render({
            elem: '#photo_index_add' //绑定元素
            ,url: '/photo/upload'//上传接口
            ,field: 'imgs'
            ,multiple: true
            ,data: {albumId: album_index_js.item.id}
            ,done: function(res){
                //上传完毕回调
                if (res.state == 'success'){
                    photo_index_js.data_init();
                    photo_index_js.layer.msg(res.message,{icon: 6});
                }else {
                    photo_index_js.layer.msg(res.message,{icon: 5});
                }
            }
            ,error: function(){
                //请求异常回调
                photo_index_js.layer.msg('上传失败',{icon: 5});
            }
        });

        $('#photo_index_delete').on('click', function () {
            if (null != photo_index_js.item){
                var _id = photo_index_js.item.id;
                photo_index_js.layer.confirm('真的删除行么', function(index){
                    $.post('/photo/delete', {id: _id}, function (data,status) {
                        if (data.state == 'success'){
                            $('#' + _id).remove();
                            photo_index_js.item = null;
                            photo_index_js.layer.msg(data.message,{icon:6});
                        }else {
                            photo_index_js.layer.msg(data.message,{icon:5});
                        }
                    });
                    photo_index_js.layer.close(index);
                });
            }else {
                photo_index_js.layer.msg('先要选中一个',{icon:5});
            }
        });

        $('#photo_index_update').on('click', function () {
            if (null != photo_index_js.item){
                var _id = photo_index_js.item.id;
                photo_index_js.layer.prompt({title: '输入名称', formType: 0, value: photo_index_js.item.name}, function(text, index){
                    $.post('/photo/update', {id: _id, name: text}, function (data,status) {
                        if (data.state == 'success'){
                            $('#' + _id).attr("data-name", text);
                            var span = $('#' + _id).find('span');
                            $(span).text(text);
                            photo_index_js.layer.msg(data.message,{icon:6});
                            photo_index_js.layer.close(index);
                        }else {
                            photo_index_js.layer.msg(data.message,{icon:5});
                        }
                    });
                });
            }else {
                photo_index_js.layer.msg('先要选中一个',{icon:5});
            }
        });

        $('#photo_album_name').text(album_index_js.item.name + '——' + album_index_js.item.postDate);
        $('#photo_album_introduce').text(album_index_js.item.introduce);
    },
    data_init: function () {
        $.post('/photo/getAll', {albumId: album_index_js.item.id}, function (res) {
            photo_index_js.laytpl(photo_index_js.tpl).render({data: res.data}, function (html) {
                $("#photo_index_content").html(html);
            });
        });
    },
    item_select: function(that){
        if (null != photo_index_js.item){
            $('#' + photo_index_js.item.id).removeClass('photo-index-item-select');
        }
        $(that).addClass('photo-index-item-select');
        photo_index_js.item = {id: $(that).attr('id'),
            name: $(that).attr('data-name'),
            introduce: $(that).attr('data-introduce'),
            postDate: $(that).attr('data-postDate'),
            state: $(that).attr('data-state')};
    },
    item_mouseenter: function(div){
        photo_index_js.layerIndex = photo_index_js.layer.tips($(div).attr('data-postDate'), div, {
            tips: 3
        });
    },
    item_mouseleave: function(){
        photo_index_js.layer.close(photo_index_js.layerIndex);
    },
    tpl: '{{# layui.each(d.data, function(index, item){ }}\n' +
    '        <div class="photo-index-item" id="{{ item.id }}"' +
    '               data-postDate="{{ item.postDate }}" data-name="{{ item.name }}" data-state="{{ item.state }}"' +
    '               onmouseenter="photo_index_js.item_mouseenter(this)" onmouseleave="photo_index_js.item_mouseleave()" onclick="photo_index_js.item_select(this)">\n' +
        '            <img src="{{ item.subUrl }}">\n' +
        '            <span>{{ item.name }}</span>\n' +
    '        </div>\n' +
    '        {{# }); }}'
}